package com.ybu.oodd.mapper;

import com.ybu.oodd.pojo.Pictures;
import com.ybu.oodd.pojo.Problem;
import com.ybu.oodd.pojo.User;
import lombok.Data;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    /*
    * 查询是否存在邮箱
    * */
    @Select("select count(*) > 0 from user where email = #{email}")
    boolean getEmail(String email);

    /*
     * 查询是否存在电话
     * */
    @Select("select count(*) > 0 from user where phone = #{phone}")
    boolean getPhone(String phone);

    /*
    * 根据email查询密码
    * */
    @Select("select pass_word from user where email = #{email}")
    String getPasswordByEmail(String email);

    /*
     * 根据phone查询密码
     * */
    @Select("select pass_word from user where phone = #{phone}")
    String getPasswordByPhone(String phone);

    /*
    * 根据email查询userid
    * */
    @Select("select id from user where email = #{email}")
    Integer getUserIdByEmail(String email);

    /*
     * 根据phone查询userid
     * */
    @Select("select id from user where phone = #{phone}")
    Integer getUserIdByPhone(String email);

    /*
     * 保存用户
     * */
    void saveUser(User user);

    /*
     * 修改密码
     * */
    @Update("update user set pass_word = #{password} where id = #{id}")
    void updatePassword(Integer id, String password);

    /*
    * 根据id查询email
    * */
    @Select("select email from user where id = #{id}")
    String getEmailByid(Integer id);

    /*
     * 根据id查询phone
     * */
    @Select("select phone from user where id = #{id}")
    String getPhoneByid(Integer id);

    /*
    * 根据id查询用户信息
    * */
    User getUserInfo(Integer id);

    /*
    * 查询vip用户集合
    * */
    @Select("select id from user where now() < job_vip_end_time")
    List<Integer> getVipUserId();

    /*
     * 查询freeNum>0 用户集合
     * */
    @Select("select id,free_num from user where free_num > 0")
    List<Map<Integer,Integer>> getFreeUserId();

    /*
     * 扣减免费查询次数
     * */
    @Update("update user set free_num = free_num - 1 where id = #{userId}")
    void downFreeNum(Integer userId);

    /*
     * 用户添加问题
     * */
    @Insert("insert into problem(user_id,question,picture,create_time) values(#{userId},#{question},#{picture},#{createTime})")
    void saveUserProblem(Problem problem);

    /*
     * 用户查询问题
     * */
    List<Problem> getUserProblem(Integer userId);

    /*
     * 用户修改问题
     * */
    void updateUserProblem(Problem problem);

    /*
     * 用户删除问题
     * */
    void deleteUserProblem(Integer userId, Integer id);

    /*
     * 用户上传文件
     * */
    void savePicture(Pictures p);

    /*
     * 根据id和user_id 查询url
     * */
    List<Pictures> getPictureUrl(Object userId, List<Integer> ids);
}
